package com.jqr.activity.basicstatedemo


import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun WaterTaskItem(
    taskName:String,
    onClose:()->Unit,
    modifier: Modifier=Modifier
){
    Row (
        modifier=modifier, verticalAlignment = Alignment.CenterVertically
    ){
        Text(text = taskName,modifier= Modifier
            .weight(1f)
            .padding(start = 16.dp))

        IconButton(onClick =  onClose ) {
            Icon(Icons.Filled.Close, contentDescription ="close" )
        }
    }

}
data class  Task(val id:Int,val label:String)

fun  getTasks()= List(30){i->Task(i,"Task # $i")}
@Composable
fun  TaskScreen(
    modifier: Modifier=Modifier,
    list:List<Task> = remember {
        getTasks()
    },
){

    LazyColumn(modifier =modifier ){

        items(list.size){i->

            StateTaskItem(list[i].label)
        }

    }

}

@Composable
fun StateTaskItem(
    taskName: String,
    modifier: Modifier=Modifier
){
    var checkedState by remember { mutableStateOf(false) }
    //var checkedState by rememberSaveable { mutableStateOf(false) }

    TaskItem(taskName = taskName, checked = checkedState, onClose = { /*TODO*/ }, onCheckedChange ={newValue->checkedState=newValue} )
}



@Composable
fun TaskItem(
    taskName: String,
    checked:Boolean,
    onClose: () -> Unit,
    onCheckedChange:(Boolean)->Unit,
    modifier: Modifier=Modifier
){
    Row(
        modifier=modifier,
        verticalAlignment = Alignment.CenterVertically

    ) {
        Text(
            text = taskName,
            modifier
                .weight(1f)
                .padding(start = 16.dp)
        )
        Checkbox(checked = checked, onCheckedChange =onCheckedChange )

        IconButton(onClick = onClose) {
           Icon(Icons.Filled.Close, contentDescription ="close" )

        }
    }

}